package No1_100.No1_10.StringtoInteger_atoi_8;

public class Solution {
    public static int myAtoi(String str) {
        if((str==null)||(str.length()==0)){
            return 0;
        }
        int flag = 0;
        double sum = 0;
        int j = 0;
        for (int i = 0; i < str.length(); i++) {
        	if (str.charAt(i) == ' ') {
				if (j==0) {
					continue;
				}
				else {
					break;
				}
			}
        	if (j == 0) {
        		if (str.charAt(i) == '-') {
        			flag = 1;
        		}
                else if(str.charAt(i) == '+'){
        			flag = 2;
        		}
                else if ((str.charAt(i)<48||str.charAt(i)>57)) {
    				return 0;
    			}
                else{
                	sum = sum * 10 + str.charAt(i) - 48;
                }
			}
        	else {
        		if ((str.charAt(i)<48||str.charAt(i)>57)) {
    				break;
    			}
            	sum = sum * 10 + str.charAt(i) - 48;
			}
        	j++;
		}
        if (flag == 1) {
			sum = -sum;
		}
        if (sum>0x7fffffff || sum<0x80000000) {
			return flag==1?0x80000000:0x7fffffff;
		}
        return (int)sum;
    }
    public static void main(String[] args) {
		System.out.println(Solution.myAtoi("18446744073709551617"));
	}
}